package com.woniu.defecttracking.controller.bugs;


import com.woniu.defecttracking.commens.Result;
import com.woniu.defecttracking.dto.AddProjectDto;
import com.woniu.defecttracking.dto.FindProjectDto;
import com.woniu.defecttracking.dto.PageProjectDto;
import com.woniu.defecttracking.param.AddProjectParam;
import com.woniu.defecttracking.param.FindProjectParam;
import com.woniu.defecttracking.param.UpdateProjectParam;
import com.woniu.defecttracking.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author 何家欣
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @GetMapping("/findOneProject/{id}")
    public Result findOneProject(@PathVariable Integer id){
        FindProjectDto project = projectService.findOneProject(id);
        if (project==null){
            return Result.getInstance(project).setMessage("查询失败").setCode(500).setData(project);

        }
        return Result.getInstance(project).setMessage("查询成功").setCode(200).setData(project);
    }

    @PostMapping("/findAllProject")
    public Result findAllProject(){
        FindProjectDto allProject = projectService.findAllProject();
        if (allProject==null){
            return Result.getInstance(allProject).setMessage("查询失败").setCode(500).setData(allProject);

        }
        return Result.getInstance(allProject).setMessage("查询成功").setCode(200).setData(allProject);
    }
    @PostMapping("/findProject")
    public Result findProject(@RequestBody FindProjectParam findProjectParam){
        System.out.println(findProjectParam.getPageIndex());
        PageProjectDto project = projectService.findProject(findProjectParam);
        if (project==null){
            return Result.getInstance(project).setMessage("查询失败").setCode(500).setData(project);

        }
        return Result.getInstance(project).setMessage("查询成功").setCode(200).setData(project);
    }

    @PostMapping("/addProject")
    public Result addProject(@RequestBody AddProjectParam addProjectParam){
        AddProjectDto project = projectService.addProject(addProjectParam);
        if (project==null){
            return Result.getInstance(project).setMessage("添加失败").setCode(500).setData(project);

        }
        return Result.getInstance(project).setMessage("添加成功").setCode(200).setData(project);
    }

    @PostMapping("/updateProject")
    public Result updateProject(@RequestBody UpdateProjectParam updateProjectParam){
        boolean b = projectService.updateProject(updateProjectParam);
        if (b==false){
            return Result.getInstance(false).setMessage("修改失败").setCode(500);

        }
        return Result.getInstance(true).setMessage("修改成功").setCode(200);
    }
    @PostMapping("/deleteProject")
    public Result deleteProject(@RequestBody List<Integer> ids){
        boolean b = projectService.deleteProject(ids);
        if (b==false){
            return Result.getInstance(false).setMessage("删除失败").setCode(500);

        }
        return Result.getInstance(true).setMessage("删除成功").setCode(200);
    }
    //开启项目
    @GetMapping("/updateStateUp/{id}")
    public Result updateStateUp(@PathVariable Integer id){
        System.out.println(id+"11111");
        boolean b = projectService.updateStateUp(id);
        if (b==false){
            return Result.getInstance(false).setMessage("开启失败").setCode(500);

        }
        return Result.getInstance(true).setMessage("开启成功").setCode(200);
    }
    //关闭项目
    @GetMapping("/updateStateDown/{id}")
    public Result updateStateDown(@PathVariable Integer id){
        System.out.println(id+"2222222");
        boolean b = projectService.updateStateDown(id);
        if (b==false){
            return Result.getInstance(false).setMessage("关闭失败").setCode(500);

        }
        return Result.getInstance(true).setMessage("关闭成功").setCode(200);
    }
}

